package org.example.leetcode;

import java.util.Arrays;

/**
 * @author: lynn
 * @Descript:
 * @date: 2021/8/4 21:33
 * @version: 1.0
 */
public class LC43 {
    public static void main(String[] args) {
        LC43 lc=new LC43();
        //char转数字 'a'-'b'
        System.out.println(lc.multiply("13","0"));
    }

    public String multiply(String num1, String num2) {
        char[] ch1=num1.toCharArray();
        char[] ch2=num2.toCharArray();
        int n1=ch1.length;int n2=ch2.length;
        char[] res=new char[n1+n2];
        Arrays.fill(res,'0');
        for (int i = n1-1; i >=0 ; i--) {
            for (int j = n2-1; j >=0 ; j--) {
                int product=(ch1[i]-'0')*(ch2[j]-'0');
                // 1 2 3 -- 0 1 2   ch1
                // 2 4 6 -- 0 1 2   ch2
                // res肯定是6位  5=(2+2+1)   res
                int tmp=res[i+j+1]-'0'+product;
                res[i+j+1]=(char)(tmp%10+'0');
                res[i+j]=(char)(res[i+j]-'0'+tmp/10+'0');
            }
        }
        StringBuilder sb=new StringBuilder();
        //fixme 如何去掉常数前面的0， 0000151-->151
        Boolean seen =false; //是否见过非0的数
        for (int i = 0; i <res.length ; i++) {
            if (res[i]=='0' && !seen) continue;
                sb.append(res[i]);
                seen=true;
        }
        return sb.length()==0?"0":sb.toString();
    }
}
